import java.util.*;
import java.io.*;

/**
 * Creates a .ics calendar 
 * @author         Hwang, Garry
 * @date           August 13, 2014
 * @bugs           Geographic position not implemented
 */
public class Calendar {
	public ArrayList<Event> eventList;
	public String version;
	//used to identify time zones
	public int timeZoneFinder;
	public String timeZone;
	//a String builder used to write to the file
	public StringBuilder file;
	
	/**
	 * constructs a calendar
	 * @param timeZ an int that is used to identify the time zone
	 */
	public Calendar(int timeZ) {
		eventList = new ArrayList<Event>();
		version = "2.0";
		file = new StringBuilder();
		if(timeZ == 1) {
			timeZone = "US/Pacific";
		}
		if(timeZ == 2) {
			timeZone = "US/Eastern";
		}
		if(timeZ == 3) {
			timeZone = "US/Central";
		}
		if(timeZ == 4) {
			timeZone = "US/Alaska";
		}
		else {
			timeZone ="Pacific/Honolulu";
		}
	}
	/**
	 * adds an event to the list
	 * @param eve event being added
	 */
	public void addEvent(Event eve) {
		eventList.add(eve);
	}
   
	 /**
	 * returns the list if events currently entered
	 * @return events
	 */
	public String getEvents() {
		StringBuilder events = new StringBuilder();
		for(int i =0; i < eventList.size(); i++) {
			events.append(eventList.get(i).getEvent());
		}
		return events.toString();
	}
	
	/**
	 * builds the calendar into a String
	 * @return String to be written into a file
	 */
	public String buildCal() {
		file.append("BEGIN:VCALENDAR");
		file.append(System.getProperty("line.separator"));
		file.append("VERSION:" + version);
		file.append(System.getProperty("line.separator"));
		file.append("PRODID:-//ICS 314//Team Yukon 1.0//EN");
		file.append(System.getProperty("line.separator"));
		file.append("TZID:" + timeZone);
		file.append(System.getProperty("line.separator"));
		for(int i = 0; i<eventList.size(); i++) {
			file.append(eventList.get(i).buildEvent().toString());
		}
		file.append("END:VCALENDAR");
		
		return file.toString();	
	}
	/**
	 * writes the content of file in an actual .ics file. Currently saves to C:/
	 */
	public void buildFile() {
		try {
			FileWriter file1 = new FileWriter("calendar.ics");
			file1.append(file);
			file1.close();
		}
		catch (Exception e) {
			System.out.println("error in creating file");
		}
	}
}

